from tkinter import Tk
from database.table import tablee
from database.table.tablee import Purchases
import tkinter as tk
import datetime

def AddPurchase():
    add_purchase_screen = Tk()
    add_purchase_screen.title('添加采购信息')
    screen_width, screen_height = add_purchase_screen.maxsize()
    # 窗口的宽和高
    width = 605
    height = 200
    centre = '%dx%d+%d+%d' % (width, height, (screen_width - width) / 2,
                              (screen_height - height) / 2)
    add_purchase_screen.geometry(centre)
    # 编号标签和文本框
    user_pno_label = tk.Label(add_purchase_screen, text='编号', font=('楷体', 14))
    user_pno_label.place(x=20, y=30)
    user_pno_text = tk.StringVar()
    user_pno_entry = tk.Entry(add_purchase_screen, textvariable=user_pno_text,
                              width=15)
    user_pno_entry.place(x=70, y=30)
    # 员工号标签和文本框
    user_eno_label = tk.Label(add_purchase_screen, text='员工号', font=('楷体', 14))
    user_eno_label.place(x=200, y=30)
    user_eno_text = tk.StringVar()
    user_eno_entry = tk.Entry(add_purchase_screen, textvariable=user_eno_text,
                               width=15)
    user_eno_entry.place(x=260, y=30)
    # 商品号标签和文本框
    user_cno_label = tk.Label(add_purchase_screen, text='商品号', font=('楷体', 14))
    user_cno_label.place(x=390, y=30)
    user_cno_text = tk.StringVar()
    user_cno_entry = tk.Entry(add_purchase_screen, textvariable=user_cno_text,
                                width=15)
    user_cno_entry.place(x=450, y=30)
    # 数量标签和文本框
    user_num_label = tk.Label(add_purchase_screen, text='数量', font=('楷体', 14))
    user_num_label.place(x=20, y=85)
    user_num_text = tk.StringVar()
    user_num_entry = tk.Entry(add_purchase_screen, textvariable=user_num_text,
                                width=15)
    user_num_entry.place(x=70, y=85)
    # 时间标签和文本框
    user_time_label = tk.Label(add_purchase_screen, text='时间', font=('楷体', 14))
    user_time_label.place(x=210, y=85)
    user_time_text = tk.StringVar()
    user_time_entry = tk.Entry(add_purchase_screen, textvariable=user_time_text,
                              width=15)
    user_time_entry.place(x=260, y=85)
    # 备注标签和文本框
    user_remarks_label = tk.Label(add_purchase_screen, text='备注', font=('楷体', 14))
    user_remarks_label.place(x=400, y=85)
    user_remarks_text = tk.StringVar()
    user_remarks_entry = tk.Entry(add_purchase_screen, textvariable=user_remarks_text,
                                  width=15)
    user_remarks_entry.place(x=450, y=85)

    # 确定按钮响应事件
    def confirm():
        id = str(user_pno_entry.get())
        purchasess = tablee.session.query(Purchases).filter(Purchases.Pno == id).all()
        data = Purchases.to_json(purchasess)
        length = len(data)
        if id != '':
            if length == 0:
                pno = int(id)
                eno = int(user_eno_entry.get())
                cno = int(user_cno_entry.get())
                num = int(user_num_entry.get())
                time = datetime.datetime.strptime(user_time_entry.get(), "%Y-%m-%d")
                remarks = str(user_remarks_entry.get())
                purse = Purchases(Pno=pno, Eno=eno,
                                 Cno=cno, Pnum=num,
                                 Pdate=time, Premarks=remarks)
                tablee.session.add(purse)
                tablee.session.commit()
                tablee.session.close()

                add = tk.Toplevel(add_purchase_screen)
                add.title("Success")
                screen_width, screen_height = add.maxsize()
                # 窗口的宽和高
                width = 240
                height = 100
                centre = '%dx%d+%d+%d' % (width, height, (screen_width - width) / 2,
                                          (screen_height - height) / 2)
                add.geometry(centre)
                label = tk.Label(add, text='添加成功！', font=('楷体', 14))
                label.place(x=60, y=15)

                def confirmm():
                    add.destroy()

                # 确定按钮
                user_confirm_button = tk.Button(add, text='确定', font=('楷体', 12),
                                                width=10, command=confirmm)
                user_confirm_button.place(x=65, y=60)
            else:
                add = tk.Toplevel(add_purchase_screen)
                add.title("Error")
                screen_width, screen_height = add.maxsize()
                # 窗口的宽和高
                width = 400
                height = 100
                centre = '%dx%d+%d+%d' % (width, height, (screen_width - width) / 2,
                                          (screen_height - height) / 2)
                add.geometry(centre)
                label = tk.Label(add, text='此编号被占用,请重新输入！', font=('楷体', 14))
                label.place(x=60, y=15)

                def confirmm():
                    add.destroy()

                # 确定按钮
                user_confirm_button = tk.Button(add, text='确定', font=('楷体', 12),
                                                width=10, command=confirmm)
                user_confirm_button.place(x=140, y=60)

        else:
            add = tk.Toplevel(add_purchase_screen)
            add.title("Error")
            screen_width, screen_height = add.maxsize()
            # 窗口的宽和高
            width = 400
            height = 100
            centre = '%dx%d+%d+%d' % (width, height, (screen_width - width) / 2,
                                      (screen_height - height) / 2)
            add.geometry(centre)
            label = tk.Label(add, text='编号不能为空，请重新输入！', font=('楷体', 14))
            label.place(x=60, y=15)

            def confirmm():
                add.destroy()

            # 确定按钮
            user_confirm_button = tk.Button(add, text='确定', font=('楷体', 12),
                                            width=10, command=confirmm)
            user_confirm_button.place(x=140, y=60)

    # 返回按钮响应事件
    def back():
        add_purchase_screen.destroy()

    # 确定按钮
    user_confirm_button = tk.Button(add_purchase_screen, text='确定', font=('楷体', 14),
                                    width=10, command=confirm)
    user_confirm_button.place(x=130, y=140)
    # 返回按钮
    user_back_button = tk.Button(add_purchase_screen, text='返回', font=('楷体', 14),
                                 width=10, command=back)
    user_back_button.place(x=370, y=140)

    add_purchase_screen.mainloop()


if __name__ == '__main__':
    AddPurchase()